package org.open.sharedRadar;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class WebServiceSyncronizer extends BroadcastReceiver {
	    private final String REMINDER_BUNDLE = "Menu"; 

	    // this constructor is called by the alarm manager.
	    public WebServiceSyncronizer(){ }

	    // you can use this constructor to create the alarm. 
	    //  Just pass in the main activity as the context, 
	    //  any extras you'd like to get later when triggered 
	    //  and the timeout
	     public WebServiceSyncronizer(Context context, Bundle extras, int timeoutInSeconds){
	         AlarmManager alarmMgr = 
	             (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
	         Intent intent = new Intent(context, WebServiceSyncronizer.class);
	         intent.putExtra(REMINDER_BUNDLE, extras);
	         PendingIntent pendingIntent =
	             PendingIntent.getBroadcast(context, 0, intent, 
	             PendingIntent.FLAG_UPDATE_CURRENT);
	         Calendar time = Calendar.getInstance();
	         time.setTimeInMillis(System.currentTimeMillis());
	         time.add(Calendar.SECOND, timeoutInSeconds);
	         alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), 1, pendingIntent);
	     }

	      @Override
	     public void onReceive(Context context, Intent intent) {
	         // here you can get the extras you passed in when creating the alarm
	         //intent.getBundleExtra(REMINDER_BUNDLE));
	    	  WebServiceHandler handler = new WebServiceHandler();
	    	  handler.updateLocation();
	    	  handler.getRadars();
	    	  handler.getFriends();
	    	  handler.getFriendsRequests();
	         Toast.makeText(context, "Local DataBase Syncronized", Toast.LENGTH_SHORT).show();
	     }
	}
